﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class RankPanel : BasePanel<RankPanel>
{
    public CustomGUIButton btnClose;
    void Start()
    {

        Hide();
        btnClose.clickEvent += () =>
        {
            Hide();
            StartPanel.Instance.Show();
        };
        GameDataMgr.Instance.LoadRankData();
        //for (int i = 0; i < 3; i++)
        //{
        //    GameDataMgr.Instance.AddRank(RandName(5), UnityEngine.Random.Range(10, 1000), UnityEngine.Random.Range(3800, 7200));
        //}
        UpdatePlaneInfo();
    }

    string RandName(int len)
    {
        string name = "";
        string[] names = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k" };
        for (int i=0; i<len; i++)
        {
            int randIndex = UnityEngine.Random.Range(0, names.Length);
            name += names[randIndex];
        }
        return name;
    }

    public void UpdatePlaneInfo()
    {
        List<Rank> ranks = GameDataMgr.Instance.rankData.ranks;
        for (int i = 0; i < ranks.Count; i++)
        {
            Transform obj = this.transform.Find(string.Format("line/rank_{0}", i + 1));
            if (obj)
            {
                CustomLabel[] labels = obj.GetComponentsInChildren<CustomLabel>();
                if (labels.Length == 4)
                {
                    labels[1].content.text = ranks[i].name;
                    labels[2].content.text = ranks[i].score.ToString();
                    labels[3].content.text = TimeUtil.Instance.TimeToString(ranks[i].time);
                }
            }
        }
    }
}
